{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "052cb52c",
   "metadata": {
    "id": "052cb52c"
   },
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/QData/spacetimeformer/blob/master/docs/2demo/fastDemo.ipynb)\n",
    " \n",
    "[![View Source on GitHub](https://img.shields.io/badge/github-view%20source-black.svg)](https://github.com/QData/spacetimeformer/blob/master/docs/2demo/fastDemo.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "t63KCxH3OVhf",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "t63KCxH3OVhf",
    "outputId": "b7e7270d-03a4-4fb1-bdab-1bccef622904"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fatal: destination path 'spacetimeformer' already exists and is not an empty directory.\n",
      "/content/spacetimeformer\n",
      "/content/spacetimeformer\n",
      "Requirement already satisfied: certifi==2020.4.5.1 in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 1)) (2020.4.5.1)\n",
      "Requirement already satisfied: joblib==0.14.1 in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 2)) (0.14.1)\n",
      "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 3)) (1.19.5)\n",
      "Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 4)) (1.1.5)\n",
      "Requirement already satisfied: python-dateutil==2.8.1 in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 5)) (2.8.1)\n",
      "Requirement already satisfied: pytz==2019.3 in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 6)) (2019.3)\n",
      "Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 7)) (0.22.2.post1)\n",
      "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 8)) (1.4.1)\n",
      "Requirement already satisfied: six==1.14.0 in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 9)) (1.14.0)\n",
      "Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from -r requirements.txt (line 10)) (4.41.1)\n",
      "Processing /content/spacetimeformer\n",
      "  Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
      "  Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
      "    Preparing wheel metadata ... \u001b[?25l\u001b[?25hdone\n",
      "Requirement already satisfied: certifi==2020.4.5.1 in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (2020.4.5.1)\n",
      "Requirement already satisfied: six==1.14.0 in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (1.14.0)\n",
      "Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (1.1.5)\n",
      "Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (4.41.1)\n",
      "Requirement already satisfied: python-dateutil==2.8.1 in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (2.8.1)\n",
      "Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (0.22.2.post1)\n",
      "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (1.19.5)\n",
      "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (1.4.1)\n",
      "Requirement already satisfied: joblib==0.14.1 in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (0.14.1)\n",
      "Requirement already satisfied: pytz==2019.3 in /usr/local/lib/python3.7/dist-packages (from spacetimeformer==0.0.0) (2019.3)\n",
      "Building wheels for collected packages: spacetimeformer\n",
      "  Building wheel for spacetimeformer (PEP 517) ... \u001b[?25l\u001b[?25hdone\n",
      "  Created wheel for spacetimeformer: filename=spacetimeformer-0.0.0-cp37-cp37m-linux_x86_64.whl size=133348 sha256=c316913c55b35022745089b29f50cb79559745006e92a0bea038af7b0b7678fc\n",
      "  Stored in directory: /tmp/pip-ephem-wheel-cache-2svexk9m/wheels/1b/bf/35/b0f99e1fd166eea045cc19321a8ee175d5f0b4a73f4acc4a76\n",
      "Successfully built spacetimeformer\n",
      "Installing collected packages: spacetimeformer\n",
      "  Found existing installation: spacetimeformer 0.0.0\n",
      "    Uninstalling spacetimeformer-0.0.0:\n",
      "      Successfully uninstalled spacetimeformer-0.0.0\n",
      "Successfully installed spacetimeformer-0.0.0\n"
     ]
    }
   ],
   "source": [
    "!git clone --recursive https://github.com/QData/spacetimeformer.git\n",
    "%cd /content/spacetimeformer\n",
    "!pwd\n",
    "!pip install -r requirements.txt\n",
    "!pip install ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "overall-aberdeen",
   "metadata": {
    "id": "overall-aberdeen"
   },
   "source": [
    "# spacetimeformer Demo\n",
    "\n",
    "Here is a quick tutorial on how to use the methods in spacetimeformer package.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "informal-drawing",
   "metadata": {
    "id": "informal-drawing"
   },
   "outputs": [],
   "source": [
    "from spacetimeformer import spacetimeformer\n",
    "\n",
    "kernel= spacetimeformer(g=3, m=2)\n",
    "\n",
    "Xtrain = [[1,0,1,0,1], [1,1,1,0,1]]\n",
    "Xtest = [[1,1,1,1,1], [1,0,1,0,1]]\n",
    "\n",
    "kernel.compute_kernel(Xtrain, Xtest)\n",
    "\n",
    "train_kernel = kernel.get_train_kernel()\n",
    "test_kernel = kernel.get_test_kernel()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f1eeddb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "heat_map = sns.heatmap(train_kernel)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "honey-effects",
   "metadata": {
    "id": "honey-effects"
   },
   "source": [
    "## Using the main spacetimeformer Class\n",
    "\n",
    "\n",
    "#### spacetimeformer.spacetimeformer( *int* g, *int* m, *int* t=-1, *bool* *approx*=False, *double* *delta*=0.025, *int* max_iters=-1 *bool* skip_variance=False)\n",
    "\n",
    "Constructor of the spacetimeformer class. This creates a spacetimeformer object with the specified parameters.\n",
    "\n",
    "*g*: Required. The overall sequence feature length. spacetimeformer will extract length-g contiguous features (or g-mers) from each training and test sequence. \n",
    "\n",
    "*m*: Required. The number of mismatch positions to insert into each of the g-mers.\n",
    "\n",
    "*t*: Optional. The number of threads to use to compute the kernel matrix.\n",
    "\n",
    "*approx* Optional. Whether to use the spacetimeformer approximation algorithm.\n",
    "\n",
    "*delta* Optional. The delta parameter to use for the approximation algorithm. Controls how quickly the algorithm converges.\n",
    "\n",
    "*int* Optional. The maximum number of iterations of the approximation algorithm to use.\n",
    "\n",
    "*skip_variance* Optional. If *max_iters* is set, the *skip_variance* flag tells spacetimeformer to iterate up to *max_iters* without performing variance computations when running.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "touched-intersection",
   "metadata": {
    "id": "touched-intersection"
   },
   "source": [
    "#### spacetimeformer.train_kernel()\n",
    "\n",
    "#### spacetimeformer.test_kernel()\n",
    "\n",
    "*train_kernel()* returns the training portion of the kernel matrix.\n",
    "*test_kernel()* returns the testing portion of the kernel matrix.\n",
    "\n",
    "\"For example, given the set up below..\"\n"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "fastDemo.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
